/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.dto.DeptDTO;
import cn.hallele.mc.sdk.portalcenter.dto.DeptQueryDTO;
import cn.hallele.mc.sdk.usercenter.common.enums.DeptStatusEnum;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.DeptBO;
import cn.hallele.ms.portalcenter.application.bo.DeptQueryBO;
import cn.hallele.ms.starter.web.framework.ActionExecutor;
import cn.hallele.ms.starter.web.framework.context.ActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/11/16 19:01
 */
@Slf4j
@Component
public class DeptDBDas extends ModelDas<ActionExecutor, DeptDTO, DeptBO> {
    protected DeptDBDas(ActionExecutor option) {
        super(option);
    }

    private static final String FIND_DEPT = "queryDeptInfoApplicationService";
    private static final String FIND_DEPT_LIST = "queryDeptListApplicationService";
    private static final String ADD_DEPT = "saveDeptApplicationService";
    private static final String UPDATE_DEPT = "updateDeptApplicationService";
    private static final String UPDATE_DEPT_STATUS = "updateDeptStatusApplicationService";
    private static final String DELETE_DEPT = "deleteDeptApplicationService";

    /**
     * 分页查询部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public Page<DeptDTO> findDeptList(DeptQueryDTO condition) {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(FIND_DEPT_LIST, convertQuery(condition), context);
        Page<DeptBO> result = context.getResult();
        return revertPage(result);
    }

    /**
     * 查询单个部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public DeptDTO findDept(DeptQueryDTO condition) {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(FIND_DEPT, convertQuery(condition), context);
        DeptBO result = context.getResult();
        return revert(result);
    }

    /**
     * 新增 部门
     *
     * @param deptDTO 部门
     */
    public void addDept(DeptDTO deptDTO) {
        this.option.execute2Resp(ADD_DEPT, convert(deptDTO), new ActionContext());
    }

    /**
     * 更新 部门
     *
     * @param deptDTO 部门
     */
    public void updateDept(DeptDTO deptDTO) {
        this.option.execute2Resp(UPDATE_DEPT, convert(deptDTO), new ActionContext());
    }

    /**
     * 更新 部门 状态
     *
     * @param id     主键
     * @param status 状态
     */
    public void updateDeptStatus(Integer id, String status) {
        this.option.execute2Resp(UPDATE_DEPT_STATUS,
                DeptBO.builder()
                        .id(id)
                        .status(DeptStatusEnum.valueOfCode(status))
                        .build(),
                new ActionContext());
    }

    /**
     * 删除 部门
     *
     * @param id 要删除的dept id
     */
    public void deleteDept(Integer id) {
        this.option.execute2Resp(DELETE_DEPT, id, new ActionContext());
    }

    /**
     * 将T转成E
     *
     * @param deptQueryDTO 被转换者
     * @return S 转换后者
     */
    private DeptQueryBO convertQuery(DeptQueryDTO deptQueryDTO) {
        return ObjectUtil.map(deptQueryDTO, d -> DeptQueryBO.builder()
                .keywords(d.getKeywords())
                .startTime(d.getStartTime())
                .endTime(d.getEndTime())
                .pageNo(d.getPageNo())
                .pageSize(d.getPageSize())
                .id(d.getId())
                .name(d.getName())
                .phone(d.getPhone())
                .status(ObjectUtil.map(d.getStatus(), DeptStatusEnum::valueOfCode))
                .build());
    }

    /**
     * 将T转成E
     *
     * @param deptDTO 被转换者
     * @return S 转换后者
     */
    @Override
    protected DeptBO convert(DeptDTO deptDTO) {
        return ObjectUtil.map(deptDTO, d -> DeptBO.builder()
                .number(d.getNumber())
                .id(d.getId())
                .name(d.getName())
                .description(d.getDescription())
                .leader(ObjectUtil.map(d.getLeader(), l -> DeptBO.Leader.builder()
                        .id(l.getId())
                        .username(l.getUsername())
                        .build()))
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(ObjectUtil.map(d.getStatus(), DeptStatusEnum::valueOfCode))
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param deptBO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected DeptDTO revert(DeptBO deptBO) {
        return ObjectUtil.map(deptBO, d -> DeptDTO.builder()
                .number(d.getNumber())
                .id(d.getId())
                .name(d.getName())
                .description(d.getDescription())
                .leader(ObjectUtil.map(d.getLeader(), l -> DeptDTO.Leader.builder()
                        .id(l.getId())
                        .username(l.getUsername())
                        .build()))
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus().getCode())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }
}
